package com.kgc.sbt.provider111;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.TrackApi;
import com.kgc.sbt.beans.Track;
import com.kgc.sbt.beans.TrackForm;
import com.kgc.sbt.mapper.TrackMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * Created on 2021/8/19.
 *  足迹操作
 */
@Service
public class TrackProvider implements TrackApi {

    @Autowired
    private TrackMapper trackMapper;

    @Override
    public List<TrackForm> selectTrack(Integer userId) {
        return trackMapper.selectTrackByUserId(userId);
    }

    /**
     * 添加足迹
     */
    @Override
    public Boolean addTrack(Integer userId, Integer productId) {

        // 先判断是否存在该条足迹
        //Integer isExist = trackMapper.selectTrack(userId, productId);

        // 若存在，更新最后浏览时间
        if (ObjectUtils.isEmpty(trackMapper.selectTrack(userId, productId))){
            // 若不存在，插入一条新记录
            Track track = Track.builder().userId(userId).productId(productId).lookTime(new Date()).build();
            return trackMapper.insert(track) > 0 ;

        } else {
            trackMapper.deleteThis(userId, productId);
            Track track = Track.builder().userId(userId).productId(productId).lookTime(new Date()).build();
            return trackMapper.insert(track) > 0;

        }

    }


    /**
     *  删除足迹
     */
    @Override
    public Boolean delTrack(Integer userId,Integer productId) {
        return trackMapper.deleteThis(userId,productId) > 0;
    }

    /**
     * 清空足迹
     */
    @Override
    public Boolean deleteTrack(Integer userId) {
        return trackMapper.deleteByUserId(userId)>0;
    }

}
